package com.hzhc.controller.json;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.entity.*;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.*;
import com.hzhc.utils.CronUtil;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.*;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/4/8 11:00
 * @description JsonController
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/base/json")
public class JsonController extends BaseController {
    private final SysDictService sysDictService;
    private final CntrSizeService sizeService;
    private final CntrTypeService typeService;
    private final CntrYardService cntrYardService;
    private final SysYzsRoleService yzsRoleService;
    private final SysWxRoleService sysWxRoleService;
    private final SysUserSecurityService securityService;
    private final CompanyInfoService companyInfoService;

    private final UserMemberService userMemberService;
    private final SysAuthService authService;
    private final YwOrderService ywOrderService;

    private final TrainPlanDetailService detailService;

    private final CarInfoService carInfoService;

    @Value("${sjRoleId}")
    private Long sjRoleId;

    /**
     * 获取Cron中文翻译
     *  @param json
     */
    @RequestMapping("/getCronEscape")
    public ResultUtil<String> getCronEscape(@RequestBody String json) {
        JSONObject object = JSONObject.parseObject(json);
        String executeCycle =  object.getString("executeCycle");
        return ResultUtil.build(CronUtil.translateToChinese(executeCycle), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 获取字典分页列表
     *
     * @param vo
     * @return
     */
    @RequestMapping("/getSysDictVoJson")
    public ResultUtil<List<SysDictVo>> getSysDictVoJson(@RequestBody SysDictVo vo) {
        Page<SysDictVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<SysDictVo> sysDictVoList = sysDictService.getSysDictVoJson(page, vo);
        return ResultUtil.build(sysDictVoList,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 集装箱新增 箱尺寸list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getCntrSizeList")
    public ResultUtil<List<CntrSizeVo>> getCntrSizeList(HttpServletRequest request,@RequestBody CntrSizeVo vo){
        List<CntrSizeVo> list = sizeService.getCntrSizeList(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 集装箱新增 箱类型list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getCntrTypeList")
    public ResultUtil<List<CntrTypeVo>> getCntrSizeList(HttpServletRequest request,@RequestBody CntrTypeVo vo){
        List<CntrTypeVo> list = typeService.getCntrType(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    @RequestMapping("/getCntrSizeTypeList")
    public ResultUtil<List<CntrSizeVo>> getCntrSizeTypeList(HttpServletRequest request,@RequestBody CntrSizeVo vo){
        List<CntrSizeVo> list = sizeService.getCntrSizeTypeList(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }
    /**
     * 集装箱新增 箱场位区域 (一级下拉框)
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getCntrYardList")
    public ResultUtil<List<CntrYardVo>> getCntrYardList(HttpServletRequest request, @RequestBody CntrYardVo vo){
        List<CntrYardVo> list = cntrYardService.getCntrYard(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 小程序角色
     */
    @RequestMapping("/getSysWxRoleJson")
    public ResultUtil<List<SysWxRole>> getSysWxRoleJson(@RequestBody SysWxRoleVo vo) {
        Page<SysWxRole> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<SysWxRole> sysWxRoleList = sysWxRoleService.getSysWxRoleJson(page, vo);
        return ResultUtil.build(sysWxRoleList, page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }


    /**
     * 一站式角色
     */
    @RequestMapping("/getYzsRoleJsonPage")
    public ResultUtil<List<SysYzsRoleVo>> getYzsRoleJsonPage(@RequestBody SysYzsRoleVo vo) {
        Page<SysYzsRoleVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<SysYzsRoleVo> sysYzsRoleVoList = yzsRoleService.getYzsRoleJsonPage(page, vo);
        return ResultUtil.build(sysYzsRoleVoList,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 计费项目新增  计费要素/计费类型
     * @param code
     * @return
     */
    @GetMapping("/getChargeElement/{code}")
    public ResultUtil<List<SysDictVo>> getChargeElement(@PathVariable("code") String code){
        List<SysDictVo> list = sysDictService.getChargeElement(code);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 表单提交时 Java代码校验密码正则
     * @param vo
     * @return
     */
    @RequestMapping("/checkPasswordLevel")
    public ResultUtil<T> checkPasswordLevel(@RequestBody SysUserSecurityVo vo){
        return securityService.checkPasswordLevel(vo.getPassword());
    }

    /**
     * 前端校验密码正则 (获取库中启用的强度等级 和 提醒)
     * @return
     */
    @GetMapping("/getStrengthLevel")
    public ResultUtil<List<SysUserSecurity>> getStrengthLevel(){
        return securityService.getStrengthLevel();
    }

    /**
     * yzs 司机扫码注册(根据id查询车队信息)
     * @param vo
     * @return
     */
    @RequestMapping("/getCarTeamById")
    public ResultUtil<CompanyInfo> getCarTeamById(@RequestBody CompanyInfoVo vo) {
        CompanyInfo companyInfo = companyInfoService.getById(vo.getId());
        return ResultUtil.build(companyInfo, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     *  小程序扫码 保存司机账号
     * @param request
     * @param data
     * @return
     */
    @PostMapping("/saveDriverMember")
    @SysTemLog(operModul = "基础信息-客户账号新增", operType = "新增", operDesc = "客户账号新增功能", modular = "PC")
    public ResultUtil<T> saveDriverMember(HttpServletRequest request, @RequestBody JSONObject data) {
        UserMemberVo vo = data.to(UserMemberVo.class);

        LambdaQueryWrapper<UserMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserMember::getUsername,vo.getUsername())
                .eq(UserMember::getDisable,0);

        UserMember one = userMemberService.getOne(wrapper);
        boolean flag = true;
        if (one==null){//没有则新增
            String field = "username";
            String username = data.getString("username");
            Boolean bool1 = userMemberService.checkUserMemberByField(field, username,null);
            if (bool1) {
                return ResultUtil.build(ResultUtilCodeEnum.LOGIN_NAME_AlREAD_EXIST);
            }

            String field1 = "telephone";
            String telephone = data.getString("telephone");
            Boolean bool2 = userMemberService.checkUserMemberByField(field1, telephone, null);
            if (bool2) {
                return ResultUtil.build(ResultUtilCodeEnum.MOBILE_AlREAD_EXIST);
            }
//            UserDto user = TokenUtil.getUser(request);
//            vo.setUserId(user.getId());
            //客户账号 密码java正则校验  开始
            ResultUtil<T> rel = securityService.checkPasswordLevel(vo.getPassword());
            if (rel.getCode()==400){
                return rel;
            }
            //客户账号 密码java正则校验  结束
            vo.setFleet(vo.getCompanyId().toString());//车队id
            vo.setCompanyId(null);//公司id
            vo.setSysWxRoleId(sjRoleId);
            vo.setInoutflag(0);//扫码默认外部
            boolean bool  = userMemberService.saveUserMember(vo);
            flag = false;
        }else{//有则更新信息+加入新车队
            userMemberService.updateDriver(vo);
            flag = false;
        }
        if(!flag){
            return ResultUtil.ok();
        }else{
            return ResultUtil.fail();
        }
    }

    //业务 授权给车队
    @PostMapping("/addYwAuth")
    public ResultUtil<T> addYwAuth(HttpServletRequest request,@RequestBody SysAuthVo vo){
        return authService.addYwAuth(request,vo);
    }

    //当前业务 所授权 的车队
    @RequestMapping("/getAuthToCom")
    public ResultUtil<List<SysAuth>> getAuthToCom(HttpServletRequest request, @RequestBody SysAuthVo vo){
        List<SysAuth> list = authService.getAuthToCom(vo);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    //业务授权--车队选择列表
    @GetMapping("/getDriverCompanyList")
    public ResultUtil<List<CompanyInfo>> getDriverCompanyList(){
        List<CompanyInfo> list = companyInfoService.getDriverCompanyList();
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    //yzs获取委托号
    @RequestMapping("/getYwOrderNoByType")
    public ResultUtil<List<YwOrderVo>> getYwOrderNoByType(HttpServletRequest request,@RequestBody YwOrderVo vo){
        Long userID = TokenUtil.getUserID(request);
        vo.setUserId(userID);
        List<YwOrderVo> list  = ywOrderService.getYwOrderNoByType(vo);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 装/卸 清单列表
     */
    @RequestMapping("/getZcAndXcDetail")
    public ResultUtil<List<TrainPlanDetailVo>> getZcAndXcDetail(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo){
        Long userID = TokenUtil.getUserID(request);
        if (vo.getCompanyName()!=null){
            vo.setConsignee(vo.getCompanyName());
        }
        Page<TrainPlanDetailVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<TrainPlanDetailVo> list = detailService.getZcAndXcDetail(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }

    //公司下-车牌列表
    @RequestMapping("/getCarNoByCom")
    public ResultUtil<List<CarInfo>> getCarNoByCom(HttpServletRequest request,@RequestBody CarInfo info){
        List<CarInfo> list = carInfoService.getCarNoByCom(info);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

}
